package com.coldlz.mmp.proxy.handlerserver;

import com.coldlz.mmp.proxy.ProxyConfig;
import com.coldlz.mmp.proxy.RecordInterceptor;
import com.coldlz.mmp.proxy.Recorder;
import com.coldlz.mmp.proxy.UserContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

import java.util.List;

public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final ProxyConfig config;

    public ServerChannelInitializer(ProxyConfig config) {
        this.config = config;// 当前连接初始化用户信息
    }

    @Override
    protected void initChannel(SocketChannel ch) {
        UserContext context = new UserContext(List.copyOf(config.getInterceptors()), config.getDirectForward(), ch);
        
        Recorder recorder = config.getRecorder();
        if (recorder != null) {
            context.setRecordInterceptor(new RecordInterceptor(recorder));
        } else {
            context.setRecordInterceptor(new RecordInterceptor());
        }
        ch.pipeline().addLast(new ConnectHandler(context));
    }
}
